package com.yanqu.road.server.cmd.activity.dunhuangshiku;

import com.yanqu.road.entity.config.goods.OpenGoodsBagResult;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogDunHuangShiKuColoring;
import com.yanqu.road.pb.dunhuangshiku.DunHuangShiKuProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.dunhuangshiku.DunHuangShiKuMgr;
import com.yanqu.road.server.manger.config.GoodsBagMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;

/**
 * @Description
 * @Author cwq
 * @Data 2021/4/19 18:05
 */
@Cmd(code = GameProtocol.S_DUN_HUANG_SHI_KU_RESTORATION_OF_MURAL_FROM_CROSS, desc = "敦煌石窟修复壁画 跨服返回区服 协议")
public class DunHuangShiKuRestorationOfMuralFromCrossCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        DunHuangShiKuProto.DunHuangShiKuRestorationMuralsToGameServerMsg req = DunHuangShiKuProto.DunHuangShiKuRestorationMuralsToGameServerMsg.parseFrom(packet.getBytes());
        ret = req.getRet();
        Property property = new Property(req.getLockPropId(), BigInteger.valueOf(req.getLockNum()));
        //恢复预扣除项
        player.getModule(PreReduceModule.class).restorePreReduce(property);
        DunHuangShiKuProto.DunHuangShiKuRestorationMuralsRespMsg.Builder respMsg = DunHuangShiKuProto.DunHuangShiKuRestorationMuralsRespMsg.newBuilder();
        if(ret == 0){
            // 扣道具
            CurrencyModule module = player.getModule(CurrencyModule.class);
            module.removeCurrency(new Property(req.getLockPropId(), BigInteger.valueOf(req.getNum())), eLogMoneyType.DunHuangShiKu, eLogMoneyType.DunHuangShiKuColouringCost);
            // 加道具
            // 获得道具
            Property rewardProperty = new Property();
            for (int i = 0; i < req.getNum(); i++) {
                OpenGoodsBagResult openGoodsBagResult = GoodsBagMgr.getRandomGoods(req.getLockPropId());
                rewardProperty.addProperty(openGoodsBagResult.getReward());
            }
            module.addCurrency(rewardProperty, eLogMoneyType.DunHuangShiKu, eLogMoneyType.DunHuangShiKuColouringReward);
            String rewardStr = PropertyHelper.parsePropertyToString(rewardProperty);
            respMsg.setReward(rewardStr);
            // 实际使用数量
            respMsg.setNum(req.getNum());
            respMsg.setFinishMuralId(req.getFinishMuralId());
            // 日志
            AutoLogMgr.add(new LogDunHuangShiKuColoring(DunHuangShiKuMgr.activityInfo.getActivityId(), player.getUserId(), req.getTeamId(), req.getRegionId(), req.getMuralId(), req.getLockPropId(), req.getNum(), rewardStr));
        }
        respMsg.setRet(ret);
        player.sendPacket(ClientProtocol.U_DUN_HUANG_SHI_KU_RESTORATION_OF_MURAL, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
    }
}
